package com.hongxun.design.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.activitys.OfflineMapActivity;

import com.hongxun.design.adapters.CitysextendsBaseExpandableListAdapter;
import com.hongxun.design.bean.CitysBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/13.
 */
public class CitysFragment extends BaseFragment {
    public ExpandableListView citys_list;
    OfflineMapActivity offlineMapActivity = null;
    CitysextendsBaseExpandableListAdapter adapter;  //数据适配器
    List<CitysBean> itemBeans;
    EditText serch_words;
    String s="";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View newsLayout = inflater.inflate(R.layout.fragment_cityslayout, container,
                false);
        offlineMapActivity = (OfflineMapActivity) getActivity();
        findView(newsLayout);

        return newsLayout;
    }

    @Override
    void findView(View v) {
        super.findView(v);
        citys_list = (ExpandableListView) v.findViewById(R.id.citys_list);
        TextView serch_button= (TextView) v.findViewById(R.id.serch_button);
        serch_words= (EditText) v.findViewById(R.id.serch_words);
        serch_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  s=serch_words.getText().toString();
                if (s.length()==0){
                    offlineMapActivity.getCityData();
                }else{
                    offlineMapActivity.search(s);
                }
                itemBeans.clear();
                itemBeans.addAll(offlineMapActivity.data);
                adapter.checkData(itemBeans);
                adapter.notifyDataSetChanged();
            }
        });
        initialization();
    }

    @Override
    void initialization() {
        super.initialization();
        offlineMapActivity.getCityData();
        itemBeans = new ArrayList<>();
        itemBeans.clear();
        itemBeans.addAll(offlineMapActivity.data);
        if (adapter == null) {
            adapter = new CitysextendsBaseExpandableListAdapter(offlineMapActivity, itemBeans);
            citys_list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        citys_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (parent.isGroupExpanded(groupPosition)) {
                    parent.collapseGroup(groupPosition);
                } else {
                    //第二个参数false表示展开时是否触发默认滚动动画
                    parent.expandGroup(groupPosition, false);
                }

                return true;
            }
        });

    }

    @Override
    public void Refresh() {
        if (adapter != null) {
            if (s.length()==0){
                offlineMapActivity.getCityData();
            }else{
                offlineMapActivity.search(s);
            }
            itemBeans.clear();
            itemBeans.addAll(offlineMapActivity.data);
            adapter.checkData(itemBeans);
            adapter.notifyDataSetChanged();
        }


    }


}
